package com.cxl.demo.mapper;

import com.cxl.demo.vo.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface GoodsMapper {
    @Insert("insert into goods(goodsName,goodsPrice,goodsScore,goodsNumber,picId,provideUserId)" +
            "values(#{goodsName},#{goodsPrice},#{goodsScore},#{goodsNumber},#{picId},#{provideUserId})")
    @Options(useGeneratedKeys = true,keyProperty = "goodsId",keyColumn = "goodsId")
    Integer AddGoods(Goods goods);
    @Select("select * from goods ")
    @Results(value={
            @Result(property = "goodsId",column = "goodsId"),
            @Result(property = "goodsName",column = "goodsName"),
            @Result(property = "goodsPrice",column = "goodsPrice"),
            @Result(property = "goodsScore",column = "goodsScore"),
            @Result(property = "goodsNumber",column = "goodsNumber"),
            @Result(property = "picId",column = "picId"),
            @Result(property = "provideUserId",column = "provideUserId"),
            @Result(property = "goodsBuyNum",column = "goodsId",
                    one = @One(select = "com.cxl.demo.mapper.BuyHistoryMapper.findGoodsBuyHistoryNum")),
            @Result(property = "provideUser",column = "userId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "goodsPic",column = "picId",
                    one=@One(select="com.cxl.demo.mapper.ImageMapper.getImageById"))
    })
    List<Goods> findGoodsList();

    @Select("select * from goods where goodsName like #{keyWord} limit #{page},#{size}")
    @Results(value={
            @Result(property = "goodsId",column = "goodsId"),
            @Result(property = "goodsName",column = "goodsName"),
            @Result(property = "goodsPrice",column = "goodsPrice"),
            @Result(property = "goodsScore",column = "goodsScore"),
            @Result(property = "goodsNumber",column = "goodsNumber"),
            @Result(property = "picId",column = "picId"),
            @Result(property = "provideUserId",column = "provideUserId"),
            @Result(property = "goodsBuyNum",column = "goodsId",
                    one = @One(select = "com.cxl.demo.mapper.BuyHistoryMapper.findGoodsBuyHistoryNum")),
            @Result(property = "provideUser",column = "provideUserId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "goodsPic",column = "picId",
                    one=@One(select="com.cxl.demo.mapper.ImageMapper.getImageById"))
    })
    List<Goods> findGoodsListByName(Map<String,Object> map);


    @Select("select count(1) from goods where goodsName like #{keyWord}")
    Integer GetGoodsNum(String keyWord);


    @Select("select * from goods where provideUserId = #{keyWord} limit #{page},#{size}")
    @Results(value={
            @Result(property = "goodsId",column = "goodsId"),
            @Result(property = "goodsName",column = "goodsName"),
            @Result(property = "goodsPrice",column = "goodsPrice"),
            @Result(property = "goodsScore",column = "goodsScore"),
            @Result(property = "goodsNumber",column = "goodsNumber"),
            @Result(property = "picId",column = "picId"),
            @Result(property = "provideUserId",column = "provideUserId"),
            @Result(property = "goodsBuyNum",column = "goodsId",
                    one = @One(select = "com.cxl.demo.mapper.BuyHistoryMapper.findGoodsBuyHistoryNum")),
            @Result(property = "provideUser",column = "provideUserId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "goodsPic",column = "picId",
                    one=@One(select="com.cxl.demo.mapper.ImageMapper.getImageById"))
    })
    List<Goods> findGoodsListByUserId(Map<String,Object> map);


    @Select("select count(1) from goods where provideUserId = #{keyWord}")
    Integer GetUserGoodsNum(Integer keyWord);


    @Select("select * from goods where goodsId=#{goodsId}")
    @Results(value={
            @Result(property = "goodsId",column = "goodsId"),
            @Result(property = "goodsName",column = "goodsName"),
            @Result(property = "goodsPrice",column = "goodsPrice"),
            @Result(property = "goodsScore",column = "goodsScore"),
            @Result(property = "goodsNumber",column = "goodsNumber"),
            @Result(property = "picId",column = "picId"),
            @Result(property = "provideUserId",column = "provideUserId"),
            @Result(property = "goodsBuyNum",column = "goodsId",
                    one = @One(select = "com.cxl.demo.mapper.BuyHistoryMapper.findGoodsBuyHistoryNum")),
            @Result(property = "provideUser",column = "provideUserId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "goodsPic",column = "picId",
                    one=@One(select="com.cxl.demo.mapper.ImageMapper.getImageById"))
    })
    Goods findGoodsById(Integer goodsId);

    @Update("update goods set goodsName= #{goodsName}, goodsPrice= #{goodsPrice}, goodsScore= #{goodsScore}" +
            ", picId= #{picId}, goodsNumber= #{goodsNumber} where goodsId=#{goodsId}")
    Integer updateGoods(Goods goods);

    @Update("update goods set goodsNumber= goodsNumber-#{param2} where goodsId=#{param1}")
    Integer updateGoodsSubNumber(Integer goodsId,Integer goodsNumber);
    @Update("update goods set goodsNumber= goodsNumber+#{param2} where goodsId=#{param1}")
    Integer updateGoodsAddNumber(Integer goodsId,Integer goodsNumber);
    @Delete("delete from goods where goodsId = #{goodsId}")
    Integer deleteGoods(Integer goodsId);
}
